package com.chixing.dao;

import com.chixing.dao.BaseDao;
import com.chixing.util.DBConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 提供了持久数据的操作模板
 */
public class JdbcTemplate<E>   {
    private RowMapper<E> rowMapper;  // String name; int age

    public JdbcTemplate(RowMapper<E> rowMapper) {
        this.rowMapper = rowMapper;
    }

    /**
     * 增删改数据
     * @param sql sql语句
     * @param args 占位符的实参
     * @return 返回受影响行数
     *
     *
     * sql : insert into dept values(?,?,?)
     *       insert into emp values(?,?,?,?,?,?,?,?)
     */
    public int update(String sql,Object... args) {
       Connection connection =  DBConn.getConnection();
        PreparedStatement pstmt = null;
        int rows = 0;
        try {
            pstmt = connection.prepareStatement(sql);
            // args 数组，遍历赋值（占位符）
            for(int i =0;i<args.length;i++){
                pstmt.setObject(i+1,args[i]);
            }

         rows = pstmt.executeUpdate(); // insert update delete


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            DBConn.closeConnection();
        }
        return rows;
    }

    // E 查询的是单个对象  queryForObject
    public E queryForObject(String sql,Object... args) {
        E e = null;
        Connection connection = DBConn.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt =  connection.prepareStatement(sql); // select * from emp where empno =  ?
            for(int i =0;i<args.length;i++){
                pstmt.setObject(i+1,args[i]);
            }

            rs =  pstmt.executeQuery();
            e = rowMapper.mapper(rs);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs !=null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(pstmt !=null) {
                try {
                    pstmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            DBConn.closeConnection();
        }


        return e;
    }


    // 查询的是多个数据  queryForList
    public List<E> queryForList(String sql,Object... args) {
        List<E> list = new ArrayList<>();
        Connection connection = DBConn.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt =  connection.prepareStatement(sql); // select * from emp where empno =  ?
            for(int i =0;i<args.length;i++){
                pstmt.setObject(i+1,args[i]);
            }

            rs =  pstmt.executeQuery();
            while(rs.next()) {
               E e = rowMapper.mapper(rs);
               list.add(e);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs !=null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(pstmt !=null) {
                try {
                    pstmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            DBConn.closeConnection();
        }


        return list;
    }


}
